<?php

namespace App\Modules\Admin\Entities\Member;

use App\Models\Model;
use App\Modules\Admin\Entities\Water\WaterDeviceModel;

class UserCardModel extends Model
{
    protected $primaryKey='user_card_id';
    protected $table='user_card';
    protected $is_delete=0;


    public function user_info(){

        return $this->hasOne(UsersModel::class,'id','user_id')->select(['id','nickname']);
    }
    public function water_card(){

        return $this->hasOne(WaterCardModel::class,'card_id');
    }
    public function water_device_info(){
        return $this->hasOne(WaterDeviceModel::class,'water_id','water_id')->select(['water_id','water_title']);
    }

    public function apparatus_info(){
        return $this->hasOne(WaterDeviceModel::class,'water_id','water_id')->select(['water_id','water_title as title']);
    }

    public function getUserByCardNo($card_no)
    {
        return $this->where(['card_code'=>$card_no])->first();
    }

    public function getUserCardByIsElectric($user_id)
    {
       return $this->where(['user_id'=>$user_id,'is_electric'=>1])->first();
    }

    public function updateUserByCardNo($card_no,$update)
    {
        return $this->where(['card_code'=>$card_no])->update($update);
    }
    public function updateUserByCardId($user_card_id,$update)
    {
        return $this->where([$this->primaryKey=>$user_card_id])->update($update);
    }


    public function scoreIncrement($id,$integral)
    {
        return $this->where(['id'=>$id])->increment('integral',number_format($integral,3));
    }
    public function remainingValueDecrement($card_code,$value)
    {
        return $this->where(['card_code'=>$card_code])->decrement('remaining_value',number_format($value,2));
    }


}
